/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class XPlatform {
    static LAYA = 0;
    static WEB = 1;
    static WX = 2;
    static isWX() {
        if (typeof wx != "undefined") {
            return true;
        }
        return false;
    }
    static CheckOutPlatform() {

        if (typeof Laya != "undefined") {
            return XPlatform.LAYA;
        }
        if (typeof wx != "undefined") {
            return XPlatform.WX;
        }
        return XPlatform.WEB;
    }
    static LOAD_TYPE_JSON = 0;
    static LOAD_TYPE_TEXT = 1;
    static LoadDataFromUrl(type, url, callbackFunc) {
        switch (XPlatform.CheckOutPlatform()) {
            case XPlatform.LAYA:
                if (type == XPlatform.LOAD_TYPE_JSON) {
                    Laya.loader.load(url, Laya.Handler.create(this, callbackFunc), null, Laya.Loader.JSON);
                }
                else if (type == XPlatform.LOAD_TYPE_TEXT) {
                    Laya.loader.load(url, Laya.Handler.create(this, callbackFunc), null, Laya.Loader.TEXT);
                }
                break;
            case XPlatform.WX://todo
                // wx.request({
                //     url: url,
                //     method: 'GET',
                //     success: (resp) => {
                //         callbackFunc(resp.data);
                //     }
                // })
                break;
            case XPlatform.WEB:
                {
                    let xhr = new XMLHttpRequest();
                    // console.log(xhr)
                    xhr.responseType = "text";
                    xhr.open("get", url, true);
                    xhr.onload = (e) => {
                        if (e.target.status == 200) {
                            if (type == XPlatform.LOAD_TYPE_JSON) {
                                callbackFunc(JSON.parse(e.target.responseText));
                            }
                            else if (type == XPlatform.LOAD_TYPE_TEXT) {
                                callbackFunc(e.target.responseText);
                            }
                        }
                    }
                    xhr.send();
                }
                break;
        }
    }
}

module.exports = {
    XPlatform
}